package com.shijichaoxing.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Created by zj 2018/3/31.
 * jackson-annotations-2.9.2.jar
 * jackson-core-2.9.2.jar
 * jackson-databind-2.9.2.jar
 * 需要用到上面三个jar包，缺一不可
 */
public class ToJSON {
    public String toJson(Object object) {
        ObjectMapper mapper = new ObjectMapper();
        String json = "";
        try {
            json = mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            System.err.println(object.getClass() + "转换为json失败！");
            e.printStackTrace();
        }
        return json;
    }

}
